// src/store/modules/app.js
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  const sidebarOpened = ref(true)
  const language = ref('zh-cn')
  const pageLoading = ref(false)
  const theme = ref('light')
  
  function toggleSidebar() {
    sidebarOpened.value = !sidebarOpened.value
  }
  
  function setLanguage(lang) {
    language.value = lang
  }
  
  function setPageLoading(loading) {
    pageLoading.value = loading
  }
  
  function toggleTheme() {
    theme.value = theme.value === 'light' ? 'dark' : 'light'
    document.documentElement.setAttribute('data-theme', theme.value)
    // 保存主题设置到 localStorage
    localStorage.setItem('admin_theme', theme.value)
  }
  
  // 初始化主题
  function initTheme() {
    const savedTheme = localStorage.getItem('admin_theme') || 'light'
    theme.value = savedTheme
    document.documentElement.setAttribute('data-theme', savedTheme)
  }
  
  return {
    sidebarOpened,
    language,
    pageLoading,
    theme,
    toggleSidebar,
    setLanguage,
    setPageLoading,
    toggleTheme,
    initTheme
  }
})